using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Slider))]
public class CustomProgress : MonoBehaviour
{
    [SerializeField] RectTransform bar;
    private RectTransform slider;

    private float progress = 0;

    private float totalWidth;
    private float height;
    
    void Awake() {
        slider = GetComponent<RectTransform>();
        totalWidth = slider.sizeDelta.x - 5;
        float height = slider.sizeDelta.y;
        this.height = height;
        bar.sizeDelta.Set(0f,height);
    }

    public float Progress {
        get {
            return progress;
        }
        set {
            if(value <= 0) {
                value = 0;
            }
            if(value >= 1) {
                value = 1;
            }
            progress = value;
            
            float x = totalWidth * progress;
            
            bar.sizeDelta = new Vector2(x,height);
        }
    }
}
